package com.well.module.demo.controller;

import com.github.pagehelper.PageInfo;
import com.well.kernel.spring.web.ResponseData;
import com.well.kernel.spring.web.easyui.Combobox;
import com.well.kernel.spring.web.easyui.Datagrid;
import com.well.kernel.util.WebUtil;
import com.well.module.system.entity.Dictionary;
import com.well.module.system.service.DictionaryService;
import com.well.module.user.entity.User;
import com.well.module.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright &copy; Well All rights reserved.
 * Author:Well
 * Date:2018-03-01
 * Description:EasyUI 控件的demo
 */

@Controller
@RequestMapping("/demo/easyui")
public class EasyUIController {
    private static Logger logger=Logger.getLogger(EasyUIController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private DictionaryService dictionaryService;

    @RequestMapping("/combobox")
    public ModelAndView combobox(ModelMap modelMap){
        return new ModelAndView("module/demo/easyui/combobox",modelMap);
    }

    @RequestMapping("/selectCombobox")
    @ResponseBody
    public Combobox selectCombobox(Dictionary dictionary){
        List<Dictionary> list=dictionaryService.selectList(dictionary);
        Combobox combobox=new Combobox();
        for(Dictionary d:list){
            combobox.add(d.getValue(),d.getText());
        }
        try{
            Thread.sleep(5000); // 休眠5秒钟
        }catch(Exception e){

        }
        return combobox;
    }

    /**
     * datagrid 控件页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/datagrid")
    public ModelAndView datagrid(ModelMap modelMap){
        return new ModelAndView("module/demo/easyui/datagrid",modelMap);
    }

    /**
     * dialog 控件页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/dialog")
    public ModelAndView dialog(ModelMap modelMap){
        return new ModelAndView("module/demo/easyui/dialog",modelMap);
    }

    /**
     * datagrid 分页查询
     * @return
     */
    @RequestMapping("/selectDatagrid")
    @ResponseBody
    public Datagrid<User> selectDatagrid(User user){
        PageInfo<User> pageInfo=userService.selectPageInfo(user);
        Datagrid<User> datagrid=new Datagrid<User>(pageInfo);
        return datagrid;
    }

    @RequestMapping(value = "/deleteByIds",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData deleteByIds(String ids){
        String[] ss=StringUtils.split(ids,",");
        for(String id:ss){
            userService.delete(id);
        }
        return new ResponseData();
    }


    /**
     * form 控件页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/form")
    public ModelAndView form(ModelMap modelMap){
        return new ModelAndView("module/demo/easyui/form",modelMap);
    }

    /**
     * easyui form post 方法
     * @return
     */
    @RequestMapping(value = "/post",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData post(User user){
        try{
            Thread.sleep(5*1000);
        }catch (Exception e){
            logger.error(e);
        }
        System.out.println(user);
        return new ResponseData(true,"操作成功！");
    }

    /**
     * layout控件页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/layout")
    public ModelAndView layout(ModelMap modelMap){
        return new ModelAndView("module/demo/easyui/layout",modelMap);
    }

    /**
     * linkbutton控件页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/linkbutton")
    public ModelAndView linkbutton(ModelMap modelMap){
        return new ModelAndView("module/demo/easyui/linkbutton",modelMap);
    }

    /**
     * 加载控件页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/loading")
    public ModelAndView loading(ModelMap modelMap){
        return new ModelAndView("module/demo/easyui/loading",modelMap);
    }

    /**
     * messager 控件页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/messager")
    public ModelAndView messager(ModelMap modelMap){
        return new ModelAndView("module/demo/easyui/messager",modelMap);
    }

    @RequestMapping("/panel")
    public ModelAndView panel(ModelMap modelMap){
        return new ModelAndView("module/demo/easyui/panel",modelMap);
    }

    /**
     * textbox 控件页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/textbox")
    public ModelAndView textbox(ModelMap modelMap){
        return new ModelAndView("module/demo/easyui/textbox",modelMap);
    }

    @RequestMapping("/window")
    public ModelAndView window(ModelMap modelMap){
        return new ModelAndView("module/demo/easyui/window",modelMap);
    }

}
